package edu.sjb.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

import edu.sjb.AppLauncher;
import edu.sjb.constants.ImageConstants;
import edu.sjb.map.FileMapLoader;
import edu.sjb.map.MapLoader;
import edu.sjb.tools.ExtensionFilter;

@SuppressWarnings("serial")
public class StartScreen extends javax.swing.JFrame {
	private JButton playButton;
	private JButton exitButton;
	private JButton loadLevelButton;
	private JButton aboutButton;
	protected MapLoader levelFile;

	public StartScreen() {
		super(AppLauncher.APPNAME);
		setResizable(false);
		setUndecorated(true);
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				playButton = new JButton();
				playButton.setLayout(null);
				getContentPane().add(playButton, "Center");
				playButton.setIcon(new ImageIcon(ImageConstants.GUI_PLAY));
				playButton.setBounds(64, 4, 56, 56);
				playButton.setEnabled(false);
				playButton.setToolTipText("¡Jugar!");
			}
			{
				exitButton = new JButton();
				getContentPane().add(exitButton, "Center");
				exitButton.setLayout(null);
				exitButton.setIcon(new ImageIcon(ImageConstants.GUI_EXIT));
				exitButton.setBounds(184, 4, 56, 56);
				exitButton.setToolTipText("Salir");
			}
			{
				loadLevelButton = new JButton();
				getContentPane().add(loadLevelButton, "Center");
				loadLevelButton.setLayout(null);
				loadLevelButton.setIcon(new ImageIcon(ImageConstants.GUI_LOAD));
				loadLevelButton.setBounds(4, 4, 56, 56);
				loadLevelButton.setToolTipText("Cargar nivel...");
			}
			{
				aboutButton = new JButton();
				getContentPane().add(aboutButton, "Center");
				aboutButton.setLayout(null);
				aboutButton.setIcon(new ImageIcon(ImageConstants.GUI_ABOUT));
				aboutButton.setBounds(124, 4, 56, 56);
				aboutButton.setToolTipText("Ayuda rápida y acerca de...");
			}
			playButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					thisActionPerformed(e);
				}

			});
			exitButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					thisActionPerformed(e);
				}
			});
			loadLevelButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					thisActionPerformed(e);
				}
			});
			aboutButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					thisActionPerformed(e);
				}
			});
			pack();

			Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
			int sizeX = 244;
			int sizeY = 64;
			int x = (screen.width - sizeX) / 2;
			int y = (screen.height - sizeY) / 2;
			this.setBounds(x, y, sizeX, sizeY);
			getContentPane().setBackground(new java.awt.Color(0, 0, 0));

		}
		catch (Exception e) {
		}
	}

	private void loadLevel() {

		JOptionPane
		        .showMessageDialog(
		                null,
		                "De cancelar la carga del nivel, se cargará uno predeterminado",
		                AppLauncher.APPNAME, JOptionPane.INFORMATION_MESSAGE);
		JFileChooser fc = new JFileChooser("./niveles/");
		fc.addChoosableFileFilter(new ExtensionFilter(
		        "Super JAVA Bros levles (*.sjl)", ".sjl"));
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int control = fc.showOpenDialog(null);
		File lectura = null;
		lectura = fc.getSelectedFile();
		if (control == 0) {
			levelFile = new FileMapLoader(lectura);
		}
		else {
			levelFile = new FileMapLoader(null);
		}
		playButton.setEnabled(true);
	}

	private void thisActionPerformed(ActionEvent evt) {
		if (evt.getSource().equals(loadLevelButton)) {
			loadLevel();
		}
		if (evt.getSource().equals(playButton)) {
			GameScreen inst = new GameScreen(levelFile.getCreatedLevel());
			inst.setVisible(true);
			dispose();
		}
		if (evt.getSource().equals(aboutButton)) {
			JOptionPane.showMessageDialog(this, AppLauncher.APPNAME + " "
			        + AppLauncher.APPVERSION + "\n"
			        + "Desarrollado por Francisco Cáneva [92817] para\n"
			        + "la materia Tecnología de la Programación.\n"
			        + "Segundo cuatrimestre - 2011\n" + "\n"
			        + "Ayuda rápida:\n"
			        + "Moverse: FLECHA IZQUIERDA y FLECHA DERECHA\n"
			        + "Saltar: X\n" + "Correr: Z", AppLauncher.APPNAME,
			        JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
			                ImageConstants.STAR));
		}
		if (evt.getSource().equals(exitButton)) {
			JOptionPane.showMessageDialog(this, "¡Gracias por jugar "
			        + AppLauncher.APPNAME + "!", AppLauncher.APPNAME,
			        JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
			                ImageConstants.STAR));
			System.exit(0);
		}
	}
}
